package com.restaurant.web;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.restaurant.domain.Inventory;
import com.restaurant.domain.Modifireinventory;
import com.restaurant.domain.Modifiremaster;
import com.restaurant.service.InventoryService;
import com.restaurant.service.ModifireinventoryService;
import com.restaurant.service.ModifiremasterService;

@Component
@RequestMapping("/action/modifierinventory.do")
@SessionAttributes("modifiremaster")
public class ModifierInventoryFromController {

	private final String queryLoadAllModifier = "from Modifiremaster where isdeleted = false";
	
	@Autowired
	private ModifireinventoryService objModifireinventoryService;
	
	@Autowired
	private ModifiremasterService objModifiremasterService;
	
	@Autowired
	private InventoryService objInventoryService;
	
	@RequestMapping(method=RequestMethod.GET)
	public String modifierInventoryGETRequest(ModelMap map){
		ArrayList<Modifiremaster> listModifiremasters = null;
		Modifiremaster modifiremaster = new Modifiremaster();
		try {
			listModifiremasters = (ArrayList<Modifiremaster>) objModifiremasterService.loadByHql(queryLoadAllModifier);
		} catch (Exception e) {
			e.printStackTrace();
		}
		map.addAttribute("listModifiremasters", listModifiremasters);
		map.addAttribute("modifiremaster", modifiremaster);
		return "ModifireInventory";
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String modifierInventoryPOSTRequest(ModelMap map, @ModelAttribute ArrayList<Modifiremaster> listModifiremasters, @ModelAttribute Modifiremaster modifiremaster, BindingResult result, HttpServletRequest request, HttpServletResponse response){
		String action = request.getParameter("action");
		
		if(action.compareTo("onselect")==0){
			if(modifiremaster.getModifireID()!=0)
				map = onSelectMenu(map, modifiremaster, request, response);
			else
				return "redirect:/modifierinventory.do";
		}else if(action.compareTo("edit")==0){
			map = onAddModifireinventory(map, modifiremaster, request, response);
		}else if(action.compareTo("del")==0){
			map = onDeleteModifireinventory(map, modifiremaster, request, response);
		}
		
		try {
			listModifiremasters = (ArrayList<Modifiremaster>) objModifiremasterService.loadByHql(queryLoadAllModifier);
		} catch (Exception e) {
			e.printStackTrace();
		}
		map.addAttribute("listModifiremasters", listModifiremasters);
		return "ModifireInventory";
	}
	
	private ModelMap onSelectMenu(ModelMap map, Modifiremaster modifiremaster, HttpServletRequest request, HttpServletResponse response){
		String modifiremasterID = modifiremaster.getModifireID().toString();
		
		String queryLoadModifireinventoryForModifiremasterID = "from Modifireinventory where modifiremaster.modifireID = " + modifiremasterID;
		
		ArrayList<Modifireinventory> listModifireinventories = null;
		try {
			listModifireinventories = (ArrayList<Modifireinventory>) objModifireinventoryService.loadByHql(queryLoadModifireinventoryForModifiremasterID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		StringBuffer inventoryIDs = new StringBuffer("(");
		for(int i=0;i<listModifireinventories.size();i++){
			inventoryIDs.append(listModifireinventories.get(i).getInventory().getIdinventoryID());
			inventoryIDs.append(",");
		}
		
		if(inventoryIDs.lastIndexOf(",")>0)
			inventoryIDs.deleteCharAt(inventoryIDs.lastIndexOf(","));
		else
			inventoryIDs.append("-1");
		inventoryIDs.append(")");
		
		String queryNotModifireInventory = "from Inventory where isdeleted = false and idinventoryID not in " + inventoryIDs ;
		
		ArrayList<Inventory> listNotModifireInventory = null;
		
		try {
			listNotModifireInventory = (ArrayList<Inventory>) objInventoryService.loadByHql(queryNotModifireInventory);
		} catch (Exception e) {
			e.printStackTrace();	
		}
		
		
				
		map.addAttribute("listModifireinventories", listModifireinventories);
		map.addAttribute("listNotModifireInventory", listNotModifireInventory);
		map.addAttribute("modifiremaster", modifiremaster);
		
		return map;
	}
	
	private ModelMap onAddModifireinventory (ModelMap map, Modifiremaster modifiremaster, HttpServletRequest request, HttpServletResponse response){
		Long modifireID = modifiremaster.getModifireID();
		
		StringBuffer ids = new StringBuffer(request.getParameter("ids"));
		ids.deleteCharAt(ids.lastIndexOf(","));
		ids.insert(0, "(");
		ids.insert(ids.length(), ")");
		
		ArrayList<Inventory> listInventories = null;
		try {
			listInventories = (ArrayList<Inventory>) objInventoryService.loadByHql("from Inventory where idinventoryID in " + ids.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Modifireinventory modifireinventory = null;
		
		for (Inventory inventory : listInventories) {
			modifireinventory = new Modifireinventory();
			modifireinventory.setInventory(inventory);
			try {
				modifireinventory.setModifiremaster(objModifiremasterService.loadByPrimaryKey(modifireID));
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			try {
				objModifireinventoryService.addOrModify(modifireinventory);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return onSelectMenu(map, modifiremaster, request, response);
	}
	
	private ModelMap onDeleteModifireinventory(ModelMap map, Modifiremaster modifiremaster, HttpServletRequest request, HttpServletResponse response){
		
		StringBuffer ids = new StringBuffer(request.getParameter("ids"));
		ids.deleteCharAt(ids.lastIndexOf(","));
		ids.insert(0, "(");
		ids.insert(ids.length(), ")");
		
		ArrayList<Modifireinventory> listModifireinventories = null;
		
		try {
			listModifireinventories = (ArrayList<Modifireinventory>) objModifireinventoryService.loadByHql("from Modifireinventory where modifireInventoryID in " + ids);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		for (Modifireinventory modifireinventory : listModifireinventories) {
			try {
				objModifireinventoryService.deleteRecord(modifireinventory);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return onSelectMenu(map, modifiremaster, request, response);
	}
}
